package com.logica.tutorial.web;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.ResourceModel;

public class AreYouSurePanel extends Panel {

	private static final long serialVersionUID = -1984599623884383887L;

	public AreYouSurePanel(String id, final ModalWindow modalWindow, final HomePage homePage, final YesNoAnswer answer) {
		super(id);
		
		Form yesNoForm = new Form("yesNoForm");
		MultiLineLabel messageLabel = new MultiLineLabel("message", new ResourceModel("label.areYouSure"));
		yesNoForm.add(messageLabel);
		
		modalWindow.setTitle("Please confirm");
        modalWindow.setInitialHeight(140);
        modalWindow.setInitialWidth(300);
        
        AjaxButton yesButton = new AjaxButton("yesButton", yesNoForm) {
        	 
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form form) {
                if (target != null) {
                    answer.setConfirmed(true);
                    homePage.addPerson(target);
                    modalWindow.close(target);
                }
            }
        };
 
        AjaxButton noButton = new AjaxButton("noButton", yesNoForm) {
 
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form form) {
                if (target != null) {
                	answer.setConfirmed(false);
                    modalWindow.close(target);
                }
            }
        };
 
        yesNoForm.add(yesButton);
        yesNoForm.add(noButton);
 
        add(yesNoForm);
	}
}
